﻿using System.ComponentModel ;
using System.Windows ;
using System.Windows.Controls ;
using System.Windows.Interactivity ;
using System.Windows.Media ;

namespace OpenLightGroup.Common.Interactivity
    {
	
        /// <summary>
        /// Toggles the state of a MediaElement between Playing and Paused.
        /// </summary>
        [Description( "Toggles the state of a MediaElement between Playing and Paused." )]
        public class PlayPauseMediaElementAction : TriggerAction<Button>
            {
		
                /// <summary>
                ///  Toggles the state of a MediaElement between Playing and Paused.
                /// </summary>
                /// <param name="parameter"></param>
                protected override void Invoke ( object parameter )
                    {

                        if ( this.TargetMediaElement.CurrentState == MediaElementState.Playing )
                        {
                            this.TargetMediaElement.Pause( ) ;
                            this.AssociatedObject.Content = this.PlayContent ;
                        }
                        else if ( this.TargetMediaElement.CurrentState == MediaElementState.Paused
                                  || this.TargetMediaElement.CurrentState == MediaElementState.Stopped )
                        {
                            this.TargetMediaElement.Play( ) ;
                            this.AssociatedObject.Content = this.PauseContent ;
                        }

                    }
     
                /// <summary>
                /// Content to display when the MediaElement is stopped or paused.
                /// </summary>
                public object PlayContent
                {
                    get
                        {
                            return this.GetValue( PlayContentProperty ) ;
                        }
                    set
                        {
                            this.SetValue( PlayContentProperty, value ) ;
                        }
                }

                /// <summary>
                /// DependencyProperty backing store for the PlayContent
                /// </summary>
                public static readonly DependencyProperty PlayContentProperty =
                DependencyProperty.Register( "PlayContent", 
                                            typeof(object), typeof(PlayPauseMediaElementAction), 
                                            new PropertyMetadata( new PropertyChangedCallback( OnPlayContentUpdated ) ) ) ;

                private static void OnPlayContentUpdated ( DependencyObject d, DependencyPropertyChangedEventArgs e )
                    {

                        ( (PlayPauseMediaElementAction)d ).PlayContent = e.NewValue ;

                    }

                /// <summary>
                /// Content to display when the MediaElement is playing.
                /// </summary>
                public object PauseContent
                {
                    get
                        {
                            return this.GetValue( PauseContentProperty ) ;
                        }
                    set
                        {
                            this.SetValue( PauseContentProperty, value ) ;
                        }
                }

                /// <summary>
                /// DependencyProperty backing store for the PlayContent
                /// </summary>
                public static readonly DependencyProperty PauseContentProperty =
                DependencyProperty.Register( "PauseContent", typeof(object), typeof(PlayPauseMediaElementAction), new PropertyMetadata( null ) ) ;

                /// <summary>
                /// Media element to play or pause.
                /// </summary>
                public MediaElement TargetMediaElement
                {
                    get
                        {
                            return (MediaElement)this.GetValue( TargetMediaElementProperty ) ;
                        }
                    set
                        {
                            this.SetValue( TargetMediaElementProperty, value ) ;
                        }
                }

                /// <summary>
                /// DependencyProperty backing store for the PlayContent
                /// </summary>
                public static readonly DependencyProperty TargetMediaElementProperty =
                DependencyProperty.Register( "TargetMediaElement", typeof(MediaElement), typeof(PlayPauseMediaElementAction), new PropertyMetadata( null ) ) ;

            }
    }